package com.canbot.u05.activity.presidemode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.adapter.HostModePerformAdapter;
import com.canbot.u05.bean.Dance;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zmp
 * 主持人中的表演模块舞蹈
 */
public class PresideDanceActivity extends BaseCompereActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

        private static final String TAG = "PresideDanceActivity";

        private List<Dance> dList;

        private GridView gridView;

        private HostModePerformAdapter list_ada;

        private int lastPosition = -1;

        private Dance selectMusic;  //选择的音频

        private String danceName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_hostmode_perform);
                initView();
                gridView = (GridView) findViewById(R.id.host_gridView);
                gridView.setOnItemClickListener(this);
                List<Dance> dList = getDataSource();
                list_ada = new HostModePerformAdapter(this, dList);
                gridView.setAdapter(list_ada);
        }

        private void initView() {
                Button btnBack = (Button) findViewById(R.id.btn_back);
                Button btnRight = (Button) findViewById(R.id.btn_right);
                btnBack.setOnClickListener(this);
                btnRight.setOnClickListener(this);
                danceName = getIntent().getStringExtra("content");
        }

        //获取数据
        private List<Dance> getDataSource() {
                dList = new ArrayList<Dance>();
                Dance music1 = new Dance("迈克杰克逊", "mj.wav", "10224");
                Dance music2 = new Dance("最炫名族风", "zxmzf.wav", "1006");
                Dance music3 = new Dance("无法停止的感觉", "music10235.mp3", "10235");
                Dance music4 = new Dance("小苹果", "xpg.mp3", "6");
                Dance music5 = new Dance("龙拳", "dzzl_u05_019_1.wav", "10225");
                Dance music6 = new Dance("蓝色多瑙河", "lsdnh.mp3", "10227");
                Dance music7 = new Dance("沧海一声笑", "chisx.mp3", "6");
                Dance music8 = new Dance("海草海草", "seaweed_dance.mp3", "10236");
                Dance music9 = new Dance("DESPACITO", "despacito.mp3", "10237");
                Dance music10 = new Dance("TROUBLE MAKER", "trouble_maker.mp3", "10238");
                Dance music11 = new Dance("红昭愿", "hzy.mp3", "10239");

                dList.add(music1);
                dList.add(music2);
                dList.add(music3);
                dList.add(music4);
                dList.add(music5);
                dList.add(music6);
                dList.add(music7);
                dList.add(music8);
                dList.add(music9);
                dList.add(music10);
                dList.add(music11);

                for (int i = 0; i < dList.size(); i++) {
                        Dance music = dList.get(i);
                        if (!TextUtils.isEmpty(danceName) && danceName.equals(music.getName())) {
                                music.setChecked(true);
                                selectMusic = music;
                                lastPosition = i;
                        }
                }
                return dList;
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.btn_right:
                                //对号
                                if (selectMusic == null) {
                                        ToastTools.newInstance(this).showText(getString(R.string.please_select_audio));
                                        return;
                                }
                                Intent intent = new Intent()
                                        .putExtra("type", AudioEvent.AUDIO_TYPE_DANCE)
                                        .putExtra("path", selectMusic.getPath())
                                        .putExtra("content", selectMusic.getName())
                                        .putExtra("actionID", selectMusic.getActionId());
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                }

        }


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (dList.get(position).isChecked()) {
                        dList.get(position).setChecked(false);
                        list_ada.updataView(position, gridView);
                        selectMusic = null;
                        MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                }
                else {
                        dList.get(position).setChecked(true);
                        list_ada.updataView(position, gridView);
                        selectMusic = dList.get(position);
                        playingVoice(selectMusic);
                }
                if (lastPosition != -1 && dList.get(lastPosition).isChecked() && lastPosition != position) {
                        dList.get(lastPosition).setChecked(false);
                        list_ada.updataView(lastPosition, gridView);
                }
                lastPosition = position;
        }

        private void playingVoice(Dance dance) {
                MapVoiceAndEyes.VoiceAndEyesInfo info = new MapVoiceAndEyes.VoiceAndEyesInfo("/ai/ai05res/a1/res/audio/dance/" + dance.getPath(), null,
                                                                                             dance.getActionId());
                MapVoiceAndEyes.playMapVoiceAndEyes(info);
        }

        @Override
        protected void onPause() {
                super.onPause();
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                MsgSendUtils.sendStringMsg(MsgType.STOP_ACTION, "stop_sound");
        }

}

